#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: bijiahao
@date: 2020/11/16 09:57
@description:
"""
import nacos
import yaml


class NacosUtil(object):
	@staticmethod
	def add_config_watcher(nacos_server_address, namespace_id, group, data_id, cb):
		client = nacos.NacosClient(nacos_server_address, namespace=namespace_id)
		client.add_config_watcher(data_id, group, cb)

	@staticmethod
	def get_server_num_and_mode_num(nacos_server_address, namespace_id, service_register_group, service_name, ip, port,
	                                cluster_name=None):
		client = nacos.NacosClient(nacos_server_address, namespace=namespace_id)
		res = client.list_naming_instance(service_name, namespace_id=namespace_id, group_name=service_register_group,
		                                  clusters=cluster_name, healthy_only=True)
		hosts = res.get('hosts')
		if hosts is None or len(hosts) == 0:
			return 0, 0
		server_num = len(hosts)
		mode_num = NacosUtil.__get_mode_num(hosts, ip, port)
		return server_num, mode_num

	@staticmethod
	def __get_mode_num(hosts, server_ip, server_port):
		all_instances_sort_by_ip_port = NacosUtil.__get_all_instances_sort_by_ip_port(hosts)
		ip_port = NacosUtil.__get_ip_port(server_ip, server_port)
		if ip_port in all_instances_sort_by_ip_port:
			index = all_instances_sort_by_ip_port.index(ip_port)
			return index
		else:
			return None

	@staticmethod
	def __get_ip_port(ip, port):
		ip_port = str(ip) + ':' + str(port)
		return ip_port

	@staticmethod
	def __get_all_instances_sort_by_ip_port(hosts):
		all_instances = []
		for host in hosts:
			ip = host.get('ip')
			port = host.get('port')
			ip_port = NacosUtil.__get_ip_port(ip, port)
			all_instances.append(ip_port)
		all_instances = sorted(all_instances, key=lambda item: item)
		return all_instances

	@staticmethod
	def remove_naming_instance(nacos_server_address, namespace_id, service_register_group, service_name, ip, port,
	                           cluster_name=None):
		client = nacos.NacosClient(nacos_server_address, namespace=namespace_id)
		client.remove_naming_instance(service_name, ip, port, group_name=service_register_group,
		                              cluster_name=cluster_name)

	@staticmethod
	def add_naming_instance(nacos_server_address, namespace_id, service_register_group, service_name, ip, port,
	                        cluster_name=None):
		client = nacos.NacosClient(nacos_server_address, namespace=namespace_id)
		client.add_naming_instance(service_name, ip, port, group_name=service_register_group, cluster_name=cluster_name)

	@staticmethod
	def get_value(nacos_server_address, namespace, group, data_id, key):
		client = nacos.NacosClient(nacos_server_address, namespace=namespace)
		content = client.get_config(data_id, group)
		data = yaml.load(content, Loader=yaml.FullLoader)
		return data.get(key)

	@staticmethod
	def send_heartbeat(nacos_server_address, namespace_id, service_register_group, service_name, ip, port,
	                   cluster_name=None):
		try:
			client = nacos.NacosClient(nacos_server_address, namespace=namespace_id)
			client.send_heartbeat(service_name, ip, port, group_name=service_register_group, cluster_name=cluster_name)
		except Exception as e:
			pass

	@staticmethod
	def get_content(nacos_server_address, namespace, group, data_id):
		client = nacos.NacosClient(nacos_server_address, namespace=namespace)
		content = client.get_config(data_id, group)
		data = yaml.load(content, Loader=yaml.FullLoader)
		return data
